package com.obtk.byproperty.service.impl;

import com.obtk.byproperty.dao.OwnerMapper;
import com.obtk.byproperty.pojo.Owner;
import com.obtk.byproperty.service.OwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class OwnerServiceImpl implements OwnerService {

    @Autowired
    private OwnerMapper ownerMapper;

    @Override
    public List<String> getCommunityName() {
        return ownerMapper.getCommunityName();
    }

    @Override
    public List<String> getBuildingName() {
        return ownerMapper.getBuildingName();
    }

    @Override
    public Owner selectOwner(Owner owner) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String localAddr = request.getLocalAddr();
        int serverPort = request.getServerPort();
        Owner owners = ownerMapper.selectOwner(owner);
        owners.setOwnerPicture("http://" + localAddr + ":" + serverPort + "/owner/" + owners.getOwnerPicture());
        System.out.println(owners.getOwnerPicture());
        return owners;
    }

    @Override
    public Owner selectOpenid(String openid) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String localAddr = request.getLocalAddr();
        int serverPort = request.getServerPort();

        Owner owner = ownerMapper.selectOpenid(openid);
        owner.setOwnerPicture("http://" + localAddr + ":" + serverPort + "/owner/" + owner.getOwnerPicture());
        System.out.println(owner.getOwnerPicture());
        return owner;
    }
}
